package com.gsit.expenditure.controller;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

import javafx.beans.binding.Bindings;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;

import com.gsit.expenditure.client.domina.FXMLBase;
import com.gsit.expenditure.client.domina.VendorBean;
import com.gsit.expenditure.domain.Vendor;
import com.gsit.expenditure.services.VendorServices;
import com.gsit.expenditure.utils.AppContextLoader;
import com.gsit.expenditure.utils.Page;
import com.gsit.expenditure.utils.ViewHandler;

public class SearchVendorController extends BaseController implements Initializable {

	
	@FXML private TextField vendorname;
	@FXML private Button searchButton;
	
	@FXML
	private TableView<Vendor> vendorTable;
	@FXML
	private TableColumn<VendorBean, String> firstnameColumn;
	@FXML
	private TableColumn<VendorBean, String> lastnameColumn;
	@FXML
	private TableColumn<VendorBean, String> addressColumn;
	@FXML
	private TableColumn<VendorBean, String> phoneColumn;
	@FXML
	private TableColumn<VendorBean, String> mobileColumn;
	
	
	private SearchVendorController vendorController = this; 
	private VendorServices vendorServices;
	private List<Vendor> vendorList;
	private VendorBean vendorBean;
	
	 @Override
		public void initialize(URL url, ResourceBundle bundle) {
		 vendorServices = AppContextLoader.getBean(VendorServices.class);
			firstnameColumn.setCellValueFactory(new PropertyValueFactory<VendorBean, String>("firstName"));
			lastnameColumn.setCellValueFactory(new PropertyValueFactory<VendorBean,String>("lastName"));
			addressColumn.setCellValueFactory(new PropertyValueFactory<VendorBean,String>("address"));
			phoneColumn.setCellValueFactory(new PropertyValueFactory<VendorBean,String>("phone"));
			mobileColumn.setCellValueFactory(new PropertyValueFactory<VendorBean,String>("mobile"));
			
			//Double Select
			/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
			vendorTable.setOnMouseClicked(new EventHandler<MouseEvent>() {
				@Override
				public void handle(MouseEvent mouseEvent) {
					
					 if(mouseEvent.getButton().equals(MouseButton.PRIMARY)){
					if (mouseEvent.getClickCount()==2) {
						Vendor vendor=vendorTable.getSelectionModel().getSelectedItem();
						if (vendor!=null) {
							final FXMLBase<VendorController> fxmlBase=ViewHandler.createFXMLBase(Page.CREATE_VENDOR);
							VendorBean vendorBean=new VendorBean(null);
							vendorBean.reLoad(vendor);
							 fxmlBase.getController().reLoad(vendorBean);
							 ViewHandler.createDialog(fxmlBase, "Update Vendor......");
							 fxmlBase.getController().getStateChange().addListener(new ChangeListener<Boolean>() {
								@Override
								public void changed(ObservableValue<? extends Boolean> arg0,Boolean oldValue, Boolean newValue) {
									if (newValue) {
										fxmlBase.getDialogStage().hide();
										vendorController.onSearch();
									}
								}
							});
							 fxmlBase.getDialogStage().showAndWait();
							}
						}
					}
				}
				});
			onSearch();
			onLoad();
	 		}
	 /*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
     private void bind(){
		
		Bindings.bindBidirectional(vendorBean.getFirstName(),vendorname.textProperty());
	}


@Override
public void onLoad() {
	ObservableList<Vendor> vendors = null ;
	if (vendorList!=null) {
		vendors=FXCollections.observableArrayList(vendorList);		
	
	vendorTable.setItems(vendors);
   }
}

@Override
public void onSearch() {
	String vendorFirstName = vendorname.getText();
	if (vendorFirstName != null && !vendorFirstName.trim().equals("")) {
		 vendorList = vendorServices.findByName(vendorFirstName);
		for (Vendor vendor1 : vendorList) {
			System.out.println("vendorsList Vendors:: "	+ vendor1.toString());
		}
	}else {
		vendorList=vendorServices.findAll();
	}
    onLoad();
  }
}


